package com.java.controller;

import com.java.entity.QuestionOption;
import com.java.service.QuestionOptionService;
import com.java.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class QuestionOptionController extends BaseController {

    @Autowired
    private QuestionOptionService questionOptionService;

    @PostMapping("questionOption/insert")
    public ResultVo insert(@RequestBody QuestionOption questionOption) {
        int rows = questionOptionService.insert(questionOption);
        return toVo(rows);
    }

    @PostMapping("questionOption/delete")
    public ResultVo delete(@RequestBody String questionOptionId) {
        if (!StringUtils.hasText(questionOptionId)){
            throw new RuntimeException("questionOptionId is null");
        }
        int rows = questionOptionService.delete(Integer.valueOf(questionOptionId));
        return toVo(rows);
    }

    @PostMapping("questionOption/update")
    public ResultVo update(@RequestBody QuestionOption questionOption) {
        int rows = questionOptionService.update(questionOption);
        return toVo(rows);
    }

    @PostMapping("questionOption/selectOne")
    public ResultVo selectOne(@RequestBody String questionOptionId) {
        if (!StringUtils.hasText(questionOptionId)){
            throw new RuntimeException("questionOptionId is null");
        }
        QuestionOption questionOption = questionOptionService.selectOne(Integer.valueOf(questionOptionId));
        return ResultVo.success(questionOption);
    }

    @PostMapping("questionOption/selectSomeByQuestionId")
    public ResultVo selectSomeByQuestionId(@RequestBody String questionId) {
        if (!StringUtils.hasText(questionId)){
            throw new RuntimeException("questionId is null");
        }
        List<QuestionOption> list = questionOptionService.selectSomeByQuestionId(Integer.valueOf(questionId));
        return ResultVo.success(list);
    }

    @PostMapping("questionOption/selectAll")
    public ResultVo selectAll() {
        List<QuestionOption> list = questionOptionService.selectAll();
        return ResultVo.success(list);
    }

}
